package com.bootdo.api.textUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * 通用方法<br>
 * 
 * 
 * @author Bill 2008.4.3
 */
public class Tools {

    public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 昨天日期
     * @return
     */
    public static String getYesterday(){
        Calendar calender = Calendar.getInstance();
        calender.add(Calendar.DATE,   -1);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); // HH� ��要大写，小写的话，变� 2小时日期� 
        String datetime = format.format(calender.getTime());
        return datetime;
    }

    public static String formatJson(String jsonVal) {
        jsonVal = Tools.NullToString(jsonVal);
        jsonVal = jsonVal.replace("\"", "\\\"");
        jsonVal = jsonVal.replace("'", "\'");
        Pattern p = Pattern.compile("\r|\n");
        jsonVal = p.matcher(jsonVal).replaceAll("");
        return jsonVal;
    }

    /**
     * 将空值转化成字符� 
     * 
     * @param str
     *            String
     * @return String
     */
    public static String NullToString(String str) {
        if (str == null || str.equals("null")) {
            str = "";
        }
        return str;
    }

    public static String NullToZero(String str) {
        if (str == null || str.equals("null") || str.trim().length() == 0) {
            str = "0";
        }
        return str;
    }

    public static String NullToString2(String str) {
        if (str == null || str.equals("null")) {
            str = "&nbsp;";
        }
        return str;
    }

    public static String NullToString3(Object str) {
        if (str == null) {
            return "&nbsp;";
        }
        else {
            return str.toString();
        }
    }

    public static String trim(String str) {
        if (str == null || str.equals("null")) {
            str = "";
        }
        return str.trim();
    }

    public static String trim(Object str) {
        if (str != null && str instanceof String) {
            return trim(str.toString());
        }
        else {
            return "";
        }
    }

    public static String trimN(String str) {
        if (str == null || str.equals("null") || str.trim().length() == 0) {
            str = "0";
        }
        return str.trim();
    }

    /**
     * 字符串是否为� 
     * 
     * @param str
     * @return
     */
    public static boolean isNullStr(String str) {
        boolean b = true;
        if (str != null && str.trim().length() > 0 && !"null".equals(str))
            b = false;
        return b;
    }

    /**
     * 正整� 
     * @param str
     * @return
     */
    public static boolean isint(String str) {
        boolean b = false;
        if (!Tools.isNullStr(str)) {
            b = Tools.check(str, "^[0-9]*[1-9][0-9]*$");
        }
        return b;
    }

    /**
     * 大于等于0的整� 
     * @param str
     * @return
     */
    public static boolean isZS(String str) {
        boolean b = false;
        if (!Tools.isNullStr(str)) {
            b = Tools.check(str, "^[0-9]*[0-9][0-9]*$");
        }
        return b;
    }

    /**
     * 格式化小� 去除小数点为零的尾数
     * 
     * @param number
     * @return
     */
    public static String delDecimal(String number) {
        if (!Tools.isNullStr(number)) {
            if (Tools.isNumeric(number)) {
                int begin = number.indexOf(".");
                String end = "";
                if (begin > 0) {// 含小� 
                    end = number.substring(begin + 1, number.length());
                    if (end != null && end.length() > 0) {
                        while (end.endsWith("0") && end.length() > 0) {
                            end = end.substring(0, end.length() - 1);
                            if (end == null)
                                end = "";
                        }
                    }
                    else {
                        end = "";
                    }
                    if (end.equals("."))
                        end = "";
                    number = number.substring(0, begin + 1) + end;
                }
            }
        }
        else {
            number = Tools.NullToString(number);
        }

        return number;
    }

    /**
     * 格式化数字字符串，小数点保留两位
     */
    public static String formatNum(String numStr) {
        // numStr.split("e")
        if (Tools.isNumeric(numStr) || (!Tools.isNullStr(numStr) && numStr.indexOf("E") > 0)) {
            // DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
            // df.applyPattern("#####################################0.00");
            // return df.format(Float.parseFloat(numStr));
            BigDecimal bd = new BigDecimal(numStr);
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            if (bd.intValue() != 0)
                return String.valueOf(bd);
            else {
                return "";
            }
        }
        else {
            return "";
        }
    }

    /*
     * ###,###.######
     */
    public static String formatNum2(String numStr, int t) {

        if (!Tools.isNullStr(numStr) && (Tools.isNumeric(numStr) || numStr.indexOf("E") > 0)
                && Double.parseDouble(numStr) != 0) {
            String format = "";
            int ii = numStr.trim().indexOf(".");
            // String tmp[] = numStr.trim().split(".");
            String f = "";
            if (t == 1 && ii > 0) {
                String ss = numStr.substring(ii + 1);
                if (ss.length() >= 1) {
                    if (ss.length() < 6) {
                        f = ".";
                        for (int j = 0; j < ss.length(); j++) {
                            f += "0";
                        }
                    }
                    else {
                        f = ".000000";
                    }
                }
            }
            if (t == 1) {
                format = "##,###,###,###,##0" + f;
            }
            else if (t == 2) {
                format = "##,###,###,###,##0.00";
            }
            DecimalFormat fmt = new DecimalFormat(format);
            numStr = fmt.format(Double.valueOf(numStr));
            if (numStr.endsWith("."))
                numStr = numStr.substring(0, numStr.indexOf("."));
            if (t == 1) {
                numStr = Tools.delDecimal(numStr);
                if (numStr.endsWith("."))
                    numStr = numStr.substring(0, numStr.indexOf("."));
                return numStr;
            }
            else {
                return (numStr);
            }
        }
        else {
            return "0.00";
        }
    }

    public static String nullToZero(String str) {
        if (Tools.isNullStr(str)) {
            return "0";
        }
        else {
            if (Tools.isNumeric(str))
                return str;
            else
                return "0";
        }
    }

    /**
     * 获取当前时间,格式：yyyy-MM-dd HH:mm:ss
     * 
     * @return String
     */
    public static String getDayTime() {
        return getDate("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * <p>函数名称�        </p>
     * <p>功能说明：获取当前时� 格式：HHmmss
     *
     * </p>
     *<p>参数说明� /p>
     * @return
     *
     * @date   创建时间� 012-12-5
     * @author 作� ：吴向钊
     */
    public static String getTime() {
        return getDate("HHmmss");
    }

    /** 
     * 取当前日� 日期格式：yyyy-MM-dd
     * * */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 取当前日期，日期格式：yyyyMMdd
     * @return
     */
    public static String getDate2() {
        return getDate("yyyyMMdd");
    }

    /**
     * 获取日期
     * <p>函数名称�        </p>
     * <p>功能说明� 
     *
     * </p>
     *<p>参数说明� /p>
     * @param fromat
     * @return
     *
     * @date   创建时间� 012-6-6
     * @author 作� ：李中俊
     */
    public static String getDate(String fromat) {
        Calendar calender = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(fromat); // HH� ��要大写，小写的话，变� 2小时日期� 
        format.setLenient(false);
        String datetime = format.format(calender.getTime());
        return datetime;
    }

    /**
     * 本月� ��� ��
     * @param sDate1
     * @return
     */
    public static Date getLastDayOfMonth(Date sDate1) {
        Calendar cDay1 = Calendar.getInstance();
        cDay1.setTime(sDate1);
        final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
        Date lastDate = cDay1.getTime();
        lastDate.setDate(lastDay);
        return lastDate;
    }

    /** 
     * 当前星期� 
     * * */
    public static String getWeek() {
        Date now = new Date();
        DateFormat df = new SimpleDateFormat("EEEEE");
        String s = df.format(now);
        return s;
    }

    /** 
     * 根据date参数返回星期� 
     * * */
    public static String getWeek(String date) {
        if (!Tools.isNullStr(date)) {
            Date now = Tools.stringToDate(date, "");
            DateFormat df = new SimpleDateFormat("EEEEE");
            String s = df.format(now);
            return s;
        }
        else {
            return date;
        }
    }

    /**
     * 特殊字符转为Html("&","<",">","\t","\r\n","\n"," ","'","\\")
     * 
     * @param s
     * @return
     */
    public static String toHtml(String s) {
        s = Replace(s, "&", "&amp;");
        s = Replace(s, "<", "&lt;");
        s = Replace(s, ">", "&gt;");
        s = Replace(s, "\t", "    ");
        s = Replace(s, "\r\n", "\n");
        s = Replace(s, "\n", "&lt;br&gt;");
        s = Replace(s, " ", "&nbsp;");
        s = Replace(s, "'", "&#39;");
        s = Replace(s, "\\", "&#92;");
        s = Replace(s, "\"", "&#34;");
        return s;
    }

    // � 
    public static String unHtml(String s) {
        s = Replace(s, "<br>", "\n");
        s = Replace(s, "&nbsp;", " ");
        s = Replace(s, "&amp;", "&");
        s = Replace(s, "&lt;", "<");
        s = Replace(s, "&gt;", ">");
        s = Replace(s, "    ", "\t");
        s = Replace(s, "\n", "\r\n");
        s = Replace(s, "&lt;br&gt;", "\n");
        s = Replace(s, "&#39;", "'");
        s = Replace(s, "&#92;", "\\");
        s = Replace(s, "&#34;", "\"");
        return s;
    }

    // Replace
    public static String Replace(String source, String oldString, String newString) {
        if (source == null)
            return null;
        StringBuffer output = new StringBuffer();
        int lengOfsource = source.length();
        int lengOfold = oldString.length();
        int posStart = 0;
        int pos;
        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));
            output.append(newString);
            posStart = pos + lengOfold;
        }
        if (posStart < lengOfsource) {
            output.append(source.substring(posStart));
        }
        return output.toString();
    }

    /**
     * 去除HTML元素
     * 
     * @param input
     * @param length
     * @return
     */
    public static String removeHTML(String input, int length) {
        if (input == null || input.trim().equals("")) {
            return "";
        }
        // 去掉� ��html元素,
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "");
        str = str.replaceAll("[(/>)<]", "");
        int len = str.length();
        if (len <= length || length <= 0) {
            return str;
        }
        else {
            str = str.substring(0, length);
            str += "......";
        }
        return str;
    }

    public static boolean check(String str, String ptn) {
        Pattern pattern = Pattern.compile(ptn);
        if (str != null)
            return pattern.matcher(str).matches();
        else
            return false;
    }

    /**
     * 是否为字母� 数字、下划线组成,且必须字母或下划线开� 长度小于等于30
     */
    public static boolean isUserName(String str) {
        if (str == null || str.length() == 0)
            return false;
        if (str.getBytes().length > 30)
            return false;
        return Tools.check(str, "^[a-zA-Z_0-9][a-zA-Z0-9|_][@][\\.]{0,30}$");
    }

    /**
     * 是否为数� 
     * 
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {

        boolean b = false;
        // return Tools.check(str, "[0-9]*");
        // ^[-]?\d+[.]?\d*$
        if (str != null) {
            int e = str.indexOf("E");
            if (e > 0) {
                String s1 = str.substring(0, e);
                String s2 = str.substring(e + 1);
                if (Tools.check(s1, "^[-]?\\d+[.]?\\d*$") && Tools.check(s2, "^[-]?\\d+[.]?\\d*$")) {
                    b = true;
                }
            }
            else {
                b = Tools.check(str, "^[-]?[0-9]+[.]?\\d*$");
            }
        }
        return b;

    }

    /**
     * 验证金额（xxxxxxxxxxxx.xx12� 小数� 2位小数）
     * @param money
     * @return
     */
    public static boolean checkMoney(String money) {
        return Tools.check(money, "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    }

    /**
     * email是否合法
     * 
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        return Tools.check(email, "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    }

    /**
     * URL是否合法
     * 
     * @param url
     * @return
     */
    public static boolean checkURL(String url) {
        return Tools.check(url,
                "(http|ftp|https)://[\\w]+(.[\\w]+)([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])");
    }

    /**
     * 帐号合法验证 字母� ��，允� -20字节，允许字母数字下划线
     * 
     * @param id
     * @return
     */
    public static boolean checkID(String id) {
        if (id == null || id.length() == 0)
            return false;
        if (id.getBytes().length > 20)
            return false;
        return Tools.check(id, "^[a-zA-Z_][a-zA-Z0-9_]{0,20}$");
    }

    /**
     * 国内电话号码
     * 
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone) {
        // return Tools.check(phone, "\\d{3}-\\d{8}|\\d{4}-\\d{7}");
        return Tools.check(phone, "(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}");
    }

    /**  
     * 手机号码验证,11位，不知道详细的手机号码段，只是验证� ��必须� 和位�  
     * */
    public static boolean checkCellPhone(String cellPhoneNr) {
        //       String reg="^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$";   
        //       String reg2="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String reg = "^\\d{11}$";//"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$";
        // || Tools.check(cellPhoneNr,reg2)
        return Tools.check(cellPhoneNr, reg);
    }

    /**
     * QQ号验� 
     * 
     * @param qq
     * @return
     */
    public static boolean checkQQ(String qq) {
        return Tools.check(qq, "[1-9][0-9]{4,}");
    }

    public static boolean checkIP(String ip) {
        return Tools.check(ip, "\\d+\\.\\d+\\.\\d+\\.\\d+");
    }

    /**
     * 身份证号验证
     * 
     * @param card
     * @return
     */
    public static boolean checkCard(String card) {
        return Tools.check(card, "^\\d{15}|\\d{17}[\\dx|\\dX]$");
    }

    /**
     * 验证单字节字� 
     * 
     * @param byteStr
     * @return
     */
    public static boolean checkSingleByte(String byteStr) {
        return Tools.check(byteStr, "^[\\x00-\\xff]+$");
    }

    /**
     * 匹配日期 格式:yyyy-MM-dd
     * 
     * @param date
     * @return
     */
    public static boolean checkDate(String date) {
        return !Tools.isNullStr(date)
                && Tools.check(date, "^\\d{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1])))$");
    }

    /**
     * 匹配日期 格式:yyyyMMdd
     * 
     * @param date
     * @return
     */
    public static boolean checkDate2(String date) {
        return !Tools.isNullStr(date)
                && Tools.check(date, "^\\d{4}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3([0|1])))$");
    }

    /**
     * 匹配日期 格式:yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @return
     */
    public static boolean checkDate3(String date) {
        return !Tools.isNullStr(date)
                && Tools.check(
                date,
                "^\\d{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1]))) ((0[0-9])|([1-2][0-9])):((0[0-9])|([1-5][0-9])):((0[0-9])|([1-5][0-9]))$");
    }

    /**
     * 匹配日期 格式:yyyy/MM/dd
     * @param date
     * @return
     */
    public static boolean checkDate4(String date) {
        return !Tools.isNullStr(date)
                && Tools.check(date, "^\\d{4}/((0[1-9])|(1[0-2]))/((0[1-9])|([1-2][0-9])|(3([0|1])))$");
    }

    /**
     * 获取参数
     * 
     * @param url
     * @param paramterName
     * @return
     */
    public static String getParamter(String url, String paramterName) {
        String res = "";
        if (url != null && !url.trim().equals("") && paramterName != null && !paramterName.trim().equals("")) {
            String regEx = "(^|&)" + paramterName + "=([^&]*)(&|$)";
            Pattern pat = Pattern.compile(regEx);
            Matcher m = pat.matcher(url);
            if (m.find()) {
                // System.out.println(m.group(2));
                res = m.group(2);
            }
        }
        return res;
    }

    /**
     * 获取参数,返回集合
     */
    public static List<String> getParamters(String url, String paramterName) {
        if (url != null) {
            String tmp[] = url.split("&");
            ArrayList<String> ary = new ArrayList<String>();
            if (tmp != null) {
                for (int i = 0; i < tmp.length; i++) {
                    String t[] = tmp[i].split("=");
                    if (t != null && t.length == 2) {
                        if (t[0].equals(paramterName)) {
                            ary.add(t[1]);
                        }
                    }
                }
            }

            return ary;
        }
        return null;
    }

    /**
     * 字符内码转换 gbk TO UTF-8
     * 
     * @param para
     *            String
     * @return String
     */
    public static String getGBKtoUTF(String para) {
        if (para == null || "".equals(para)) {
            return "";
        }
        para = para.trim();
        String strtmp = "";
        try {
            strtmp = new String(para.getBytes("GBK"), "utf-8");
        }
        catch (Exception uee) {
            uee.printStackTrace();
            return "";
        }
        return strtmp;
    }

    /**
     * 返回毫秒
     * 
     * @param date
     *            日期
     * @return 返回毫秒
     */
    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 日期相减
     *
     * @param date
     *            日期
     * @param date1
     *            日期
     * @return 返回相减后的日期
     */
    public static int diffDate(Date date, Date date1) {
        return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
    }

    //    public static int diffSec(java.util.Date date, java.util.Date date1) {
    //	return (int) ((getMillis(date) - getMillis(date1)) / (1000));
    //    }

    /**
     * 日期相减，返回秒� 
     * @param date
     * @param date1
     */
    public static float diffSec(Date date, Date date1) {
        return (float) (((float) (getMillis(date) - getMillis(date1))) / (1000));
    }

    /**
     * 日期相减，返回分� 
     * <p>函数名称�        </p>
     * <p>功能说明� 
     *
     * </p>
     *<p>参数说明� /p>
     * @param date
     * @param date1
     * @return
     *
     * @date   创建时间� 011-11-28
     * @author 作� ：李中俊
     */
    public static int diffMinute(Date date, Date date1) {
        return (int) ((getMillis(date) - getMillis(date1)) / (60 * 1000));
    }

    /**
     * 日期相加
     *
     * @param date
     *            日期
     * @param day
     *            天数
     * @return 返回相加后的日期
     */
    public static Date addDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /*
     * 日期相减
     */
    public static Date decDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /**
     * 根据格式得到格式化后的日� 
     *
     * @param currDate
     *            要格式化的日� 
     * @param format
     *            日期格式，如yyyy-MM-dd
     * @see SimpleDateFormat#parse(String)
     * @return Date 返回格式化后的日期，格式由参� code>format</code>定义，如yyyy-MM-dd，如2006-02-15
     */
    public static Date getFormatDate(String currDate, String format) {
        SimpleDateFormat dtFormatdB = null;
        try {
            dtFormatdB = new SimpleDateFormat(format);
            return dtFormatdB.parse(currDate);
        }
        catch (Exception e) {
            dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
            try {
                return dtFormatdB.parse(currDate);
            }
            catch (Exception ex) {
            }
        }
        return null;
    }

    /*
     * public static String formatDate(String date, String sourceFormat, String
     * aimFormat) { SimpleDateFormat sdf = null; try { sdf = new
     * SimpleDateFormat(aimFormat); return sdf.format(sdf.parse(date)); } catch
     * (Exception e) { e.printStackTrace(); } return null; }
     */
    public static String formatDate(String date, String sourceFormat, String aimFormat) {
        if (Tools.isNullStr(date))
            return date;
        SimpleDateFormat sdfa = null;
        SimpleDateFormat sdfs = null;
        try {
            sdfa = new SimpleDateFormat(aimFormat);
            sdfs = new SimpleDateFormat(sourceFormat);
            date = sdfa.format(sdfs.parse(date));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 查找字段在字段字符串(以� 号隔� 中的逗号位置(下标� 算起)
     *
     * @param fieldStr
     * @param fieldName
     * @return
     */
    public static int findFieldIndex(String fieldStr, String fieldName) {
        int i = fieldStr.indexOf(fieldName);
        if (i >= 0) {
            String substring = fieldStr.substring(0, i);
            if (substring.trim().length() > 0 && substring.indexOf(",") > 0) {
                return substring.split(",").length;
            }
            else {
                return 0;
            }
        }
        else {
            return -1;
        }
    }

    /**
     * 过滤SQL关字
     *
     * @return
     */
    public static String filterSQL(String param) {
        // boolean r = false;
        if (param != null && param.trim().length() > 0) {
            String filter = "or,and,exec,insert,select,delete,update,count,%,chr,mid,master,truncat,char,declare";
            String filterAry[] = filter.split(",");
            // System.out.println(filterAry.length);
            for (int i = 0; i < filterAry.length; i++) {
                param = param.replaceAll(filterAry[i], "");
            }
        }
        return param;
    }

    /**
     * 参数是否含有SQL语句关健� 
     *
     * @param args
     */
    public static boolean isLawlessChar(String param) {
        boolean b = false;
        if (param != null && param.trim().length() > 0) {
            String filter = "or,and,exec,insert,select,delete,update,count,%,chr,mid,master,truncat,char,declare";
            String filterAry[] = filter.split(",");
            for (int i = 0; i < filterAry.length; i++) {
                if (param.indexOf(filterAry[i]) > 0) {
                    return true;
                }
            }
        }
        return b;
    }

    public static Date stringToDate(String date, String format) {
        Date dd = null;
        if (date == null || date.trim().length() == 0)
            return null;
        if (format == null || format.trim().length() == 0)
            format = "yyyy-MM-dd hh:mm:ss";
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            dd = formatter.parse(date);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }

        return dd;
    }

    public static String formatDate(String a[]) {
        String date = "";
        if (a != null && a.length > 1) {
            if (a[1].length() == 1)
                a[1] = "0" + a[1];
            if (a.length > 3 && a[2].length() == 1)
                a[2] = "0" + a[2];
            if (a.length > 3)
                date = a[0] + a[1] + a[2];
            else {
                date = a[0] + a[1];
            }
        }
        return date;
    }

    public static String formatDate(Date Parameter, String format) {
        SimpleDateFormat sdFormat = new SimpleDateFormat(format);
        String getDate;
        if (Parameter != null) {
            getDate = sdFormat.format(Parameter);
            if (!getDate.equals("0000-00-00"))
                return getDate;
            else
                return "";
        }// if !=null
        else
            return "";
    }

    public static String method(String date) {
        if (!Tools.isNullStr(date)) {
            String a1[] = date.split("-");
            if (a1 != null && a1.length > 1) {
                String t = "";
                for (int i = 0; i < a1.length; i++) {
                    if (a1[i].length() <= 1)
                        a1[i] = "0" + a1[i];
                    t += a1[i];
                }
                date = t;
            }
        }
        return date;
    }

    

    /**
     * 格式化参� 去空，去单引� null� "
     * 
     * @param args
     */
    public static String formatParam(String param) {
        param = Tools.trim(param);
        param = param.replaceAll("'", "''");
        return param;
    }

   
    /**
     * 生成� ��模糊查询SQL
     * @param str
     * @return
     */
    public static String simpleMohu(String str) {
        if (str == null || str.length() == 0)
            return "%";
        else
            return "%" + str + "%";
    }

    

    /**
     * 取当前周� ��日期和结束日� 周日为一周开� 
     * 
     * @return
     */
    public static String[] getDateOfThisWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        int index = cal.get(Calendar.DAY_OF_WEEK); // 今天是本周的第几� 
        // 转成中国的习� 如果是第� ��,则转化为第七� 星期天外国为� ��的第� ��,而中国为每周的最后一� 
        // if (index == 1)
        // index = 8;
        cal.add(Calendar.DATE, -(index - 1));// index - 2 (以星期一为一周开� 
        String start = (sdf.format(cal.getTime()));
        cal.add(Calendar.DATE, 6);
        String end = (sdf.format(cal.getTime()));
        String[] result = new String[] { start, end };
        return result;
    }

    /**
     * 产生随机字符a-zA-Z0-9
     * <p>函数名称�        </p>
     * <p>功能说明� 
     *
     * </p>
     *<p>参数说明� /p>
     * @return
     * len为字符长� 
     * @date   创建时间� 011-3-18
     * @author 作� ：李中俊
     */
    public static String getRndCode(int len) {
        if (len <= 0)
            return "";
        StringBuffer code = new StringBuffer();
        String src[] = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
                .split(",");
        int srcsize = src.length;
        for (int i = 0; i < len; i++) {
            Random ran = new Random();
            int index = (int) (ran.nextFloat() * srcsize);
            code.append(src[index]);
        }
        return code.toString();
    }

    /**
     * <p>函数名称�        </p>
     * <p>功能说明：解� 
     *
     * </p>
     *<p>参数说明� /p>
     * @param inputByte
     * @return
     * @throws IOException
     *
     * @date   创建时间� 015-1-16
     * @author 作� ：吴向钊
     */
    public static byte[] inflater(byte[] inputByte) throws IOException {
        int compressedDataLength = 0;
        Inflater compresser = new Inflater(false);
        compresser.setInput(inputByte, 0, inputByte.length);
        ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
        byte[] result = new byte[1024];
        try {
            while (!compresser.finished()) {
                compressedDataLength = compresser.inflate(result);
                if (compressedDataLength == 0) {
                    break;
                }
                o.write(result, 0, compressedDataLength);
            }
        }
        catch (Exception ex) {
            System.err.println("Data format error!\n");
            ex.printStackTrace();
        }
        finally {
            o.close();
        }
        compresser.end();
        return o.toByteArray();
    }

    /**
     * <p>函数名称�        </p>
     * <p>功能说明：压� 
     *
     * </p>
     *<p>参数说明� /p>
     * @param inputByte
     * @return
     * @throws IOException
     *
     * @date   创建时间� 015-1-16
     * @author 作� ：吴向钊
     */
    public static byte[] deflater(byte[] inputByte) throws IOException {
        int compressedDataLength = 0;
        Deflater compresser = new Deflater();
        compresser.setInput(inputByte);
        compresser.finish();
        ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
        byte[] result = new byte[1024];
        try {
            while (!compresser.finished()) {
                compressedDataLength = compresser.deflate(result);
                o.write(result, 0, compressedDataLength);
            }
        }
        finally {
            o.close();
        }
        compresser.end();
        return o.toByteArray();
    }
    /**
     * <p>函数名称�        </p>
     * <p>功能说明� 
     *	校验长度
     * </p>
     *<p>参数说明� /p>
     * @param str
     * @param min
     * @param max
     * @return
     *
     * @date   创建时间� 015-2-13
     * @author 作� ：bslzj
     */
    public static boolean  checkLength(String str,int min,int max){
    	long len = 0;
    	if(str!=null) len = str.getBytes().length;
    	return len>=min && len<=max;
    }
    
    /**
     * 格式化src字符串到len长度，超过len长度截取，不足长度用target在尾部补� 
     * @param src
     * @param len
     * @param target
     * @return
     */
    public static String formatStrlen(String src,int len,String target){
    	if(src==null) src="";
    	if(src.length()>=len) return src.substring(0, len);
    	do{
    		src +=target;
    	}while(src.length()<len);
    	return src;
    }
    
    /**
     * � ��密码强度是否符合
     * 包含字母和数� 
     * @param pwd
     * @return
     */
    public static boolean checkPwd(String pwd){
    	boolean b = false;
    	//Tools.check(pwd,".*[^a-zA-Z0-9].*") && 
     	if (Tools.check(pwd, ".*[0-9].*")
     			&& Tools.check(pwd, ".*[a-zA-Z].*") && pwd.length()>5){
     		b = true;
    	}
    	return b;
    }
    
    /*
     * 产生随机数，4字节随机� 4字节0
     * 
     * @return 
     */
    public static String getRandom(){
    	java.util.Random r=new java.util.Random();   	
    	String res = Integer.toHexString(r.nextInt()).toUpperCase();
    	return res+"00000000";
    }
    
    public static void main(String[] args){
    	
    	for(int i = 0;i <20;i++){
    		
    		System.out.println(getRandom());
    	}
    	
    	
    }
    
    
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    
    private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}
 
	// 完整的判断中文汉字和符号
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i <ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}

}